package com.taa.service.impl;

import com.taa.dao.IUserInfoDao;
import com.taa.dao.po.UserInfo;
import com.taa.dto.UserLoginDTO;
import com.taa.dto.UserRegisterDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: taa
 * @Desc: 用户服务实现类
 * @Create: 2025-06-22 15:40
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserInfoDao userInfoDao;

    // Spring Security的密码编码器
    private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public Boolean register(UserRegisterDTO registerDTO) {
        // 1. 参数校验
        if (!StringUtils.hasText(registerDTO.getUsername()) || !StringUtils.hasText(registerDTO.getPassword())) {
            // 【已修正】使用正确的构造函数
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        // 2. 检查用户名是否已存在
        UserInfo existingUser = userInfoDao.findByUsername(registerDTO.getUsername());
        if (existingUser != null) {
            // 【已修正】使用正确的构造函数
            throw new AppException(ResponseCode.USERNAME_ALREADY_EXISTS.getCode(), ResponseCode.USERNAME_ALREADY_EXISTS.getInfo());
        }

        // 3. 对密码进行加密处理
        String encryptedPassword = passwordEncoder.encode(registerDTO.getPassword());

        // 4. 创建PO对象，准备插入数据库
        UserInfo newUser = new UserInfo();
        newUser.setUsername(registerDTO.getUsername());
        newUser.setPassword(encryptedPassword);

        // 5. 调用DAO执行插入
        int result = userInfoDao.insert(newUser);

        // 6. 判断插入结果
        if (result > 0) {
            return true;
        } else {
            // 【已修正】使用正确的构造函数
            throw new AppException(ResponseCode.USER_REGISTER_FAILED.getCode(), ResponseCode.USER_REGISTER_FAILED.getInfo());
        }
    }

    @Override
    public Boolean login(UserLoginDTO loginDTO) {
        // 1. 参数校验
        if (!StringUtils.hasText(loginDTO.getUsername()) || !StringUtils.hasText(loginDTO.getPassword())) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        // 2. 查询用户信息
        UserInfo user = userInfoDao.findByUsername(loginDTO.getUsername());
        if (user == null) {
            throw new AppException(ResponseCode.USER_NOT_EXIST.getCode(), ResponseCode.USER_NOT_EXIST.getInfo());
        }

        // 3. 校验密码
        boolean passwordMatches = passwordEncoder.matches(loginDTO.getPassword(), user.getPassword());
        if (!passwordMatches) {
            throw new AppException(ResponseCode.USER_PASSWORD_ERROR.getCode(), ResponseCode.USER_PASSWORD_ERROR.getInfo());
        }

        // 4. 登录成功
        return true;
    }
}

